package com.zhangzhan.gulimallproduct.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhangzhan.gulimallcommcon.util.MyCollUtil;
import com.zhangzhan.gulimallproduct.dao.ItemDao;
import com.zhangzhan.gulimallproduct.entity.SkuImagesEntity;
import com.zhangzhan.gulimallproduct.entity.SkuInfoEntity;
import com.zhangzhan.gulimallproduct.entity.SpuInfoDescEntity;
import com.zhangzhan.gulimallproduct.feign.SeckillFeignService;
import com.zhangzhan.gulimallproduct.service.ItemService;
import com.zhangzhan.gulimallproduct.service.SkuImagesService;
import com.zhangzhan.gulimallproduct.service.SkuInfoService;
import com.zhangzhan.gulimallproduct.service.SpuInfoDescService;
import com.zhangzhan.gulimallproduct.vos.SkuItemSaleAttrVo;
import com.zhangzhan.gulimallproduct.vos.SkuItemVo;
import com.zhangzhan.gulimallproduct.vos.SkuSeckillTo;
import com.zhangzhan.gulimallproduct.vos.SpuItemAttrGroupVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/26 20:21
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private SkuInfoService skuInfoService;

    @Resource
    private SkuImagesService skuImagesServices;

    @Resource
    private ItemDao itemDao;

    @Resource
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SeckillFeignService seckillFeignService;


    @Override
    public SkuItemVo getItemData(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //sku基础信息
            SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, executor);

        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> {
            //spu的销售属性组合
            if (!Objects.isNull(res)) {
                List<SkuItemSaleAttrVo> saleAttrVos = itemDao.getSaleAttr(res.getSpuId());
                skuItemVo.setSaleAttr(saleAttrVos);
            }
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            //spu商品介绍信息
            if (!Objects.isNull(res)) {
                SpuInfoDescEntity desc = spuInfoDescService.getOne(Wrappers.<SpuInfoDescEntity>lambdaQuery()
                        .eq(SpuInfoDescEntity::getSpuId, res.getSpuId()));
                skuItemVo.setDesc(desc);
            }
        }, executor);

        CompletableFuture<Void> baseFuture = infoFuture.thenAcceptAsync((res) -> {
            //spu规格属性信息
            if (!Objects.isNull(res)) {
                List<SpuItemAttrGroupVo> spuItemAttrGroupVos = itemDao.getGroupAttrs(skuId, res.getSpuId());
                skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
            }
        }, executor);

        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            //sku图片集
            List<SkuImagesEntity> imagesEntities = skuImagesServices.list(Wrappers.lambdaQuery(SkuImagesEntity.class)
                    .eq(SkuImagesEntity::getSkuId, skuId));
            skuItemVo.setImages(imagesEntities);
        }, executor);


        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            //秒杀信息
            List<SkuSeckillTo> skuCeckill = seckillFeignService.getSkuCeckill(skuId);
            if (MyCollUtil.isNotEmpty(skuCeckill)) {
                SkuSeckillTo to = null;

                if (Objects.equals(skuCeckill.size(),1)){
                    to = skuCeckill.get(0);
                }else {
                    Object[] starts = skuCeckill.stream().map(SkuSeckillTo::getStartTime).toArray();
                    Arrays.sort(starts);
                    Object start = starts[0];
                    //如果不是当前正在秒杀的就获取最近要开始秒杀的
                    for (SkuSeckillTo skuSeckillTo : skuCeckill) {
                        if (StrUtil.isNotBlank(skuSeckillTo.getRandomValues())) {
                            to = skuSeckillTo;
                            break;
                        } else {
                            if (Objects.equals(start, skuSeckillTo.getStartTime())) {
                                to = skuSeckillTo;
                                break;
                            }
                        }
                    }
                }
                skuItemVo.setSkuSeckillTo(to);
            }
        }, executor);


        //阻塞执行
        CompletableFuture.allOf(attrFuture, descFuture, baseFuture, imagesFuture, seckillFuture).get();


        return skuItemVo;
    }
}
